package com.miyu.book.utils.bookUtils;

import com.alibaba.fastjson.JSONObject;
import com.miyu.book.utils.magic.GetBeanUtil;
import com.miyu.book.utils.redis.redisUtils.util.RedisListUtil;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class saveRedisThead implements Callable<String> {

    private String type;

    RedisListUtil rv = (RedisListUtil) GetBeanUtil.getBeanByClass(RedisListUtil.class);
    ExecutorService svr = Executors.newCachedThreadPool();
    @Override
    public String call() throws Exception {
        //Thread.sleep(10000);
        System.out.println(">>"+type);
        System.out.println(">>>>>>>"+rv.size(1,type));

        int  num =0;//初始话 超过10 不再执行 10*10 100秒没有新数据进入不再处理数据
        CheckAndSave(num);

        return null;
    }

    private void CheckAndSave(int num) throws Exception  {
        if(rv.size(1,type)>0){
            Thread.sleep(1000);
            JSONObject jo = rv.popLeft(1, type, JSONObject.class);
            num=0;
           // System.out.println(rv.size(1,type));
            svr.submit(new  InitSaveThead(jo));
            CheckAndSave(num);
        }else {
            Thread.sleep(10000);
            num=num+1;
            System.out.println("num>>>"+num);
            if(num>=10){
                return;
            }else{
                CheckAndSave(num);
            }
        }
    }



    public  saveRedisThead(String type){
        this.type=type;
    }


}
